package team.fjut.cf.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import team.fjut.cf.mapper.ContestRegisterMapper;
import team.fjut.cf.mapper.UserRealNameAuthMapper;
import team.fjut.cf.mapper.VContestUserMapper;
import team.fjut.cf.pojo.enums.ContestKind;
import team.fjut.cf.pojo.enums.ContestReviewStatus;
import team.fjut.cf.pojo.po.ContestInfoPO;
import team.fjut.cf.pojo.po.ContestRegisterUserPO;
import team.fjut.cf.pojo.po.UserRealNameAuth;
import team.fjut.cf.pojo.po.VContestUser;
import team.fjut.cf.pojo.vo.ContestRegisterUserVO;
import team.fjut.cf.pojo.vo.UserSignUpVO;
import team.fjut.cf.service.ContestInfoService;
import team.fjut.cf.service.ContestRegisterService;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author zhongml [2020/4/23]
 */
@Service
public class ContestRegisterServiceImpl implements ContestRegisterService {
    @Resource
    ContestRegisterMapper contestRegisterMapper;

    @Resource
    ContestInfoService contestInfoService;

    @Resource
    VContestUserMapper vContestUserMapper;

    @Resource
    UserRealNameAuthMapper userRealNameAuthMapper;

    @Override
    public List<ContestRegisterUserVO> pagesByConditions(Integer pageNum, Integer pageSize, String sort, Integer contestKind, Integer reviewStatus, String username, Integer contestId) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(ContestRegisterUserPO.class);
        List<ContestRegisterUserVO> result = new ArrayList<>();

        if (sort != null && sort.equals("descending")) {
            example.orderBy("contestId").desc();
        } else {
            example.orderBy("contestId").asc();
        }
        Example.Criteria criteria = example.createCriteria();

        if (Objects.nonNull(contestKind)) {
            criteria.andEqualTo("contestKind", contestKind);
        }
        if (Objects.nonNull(contestKind)) {
            criteria.andEqualTo("contestId", contestId);
        }
        if (Objects.nonNull(reviewStatus)) {
            criteria.andEqualTo("reviewStatus", reviewStatus);
        }
        if (Objects.nonNull(username)) {
            criteria.andLike("username", username);
        }

        List<ContestRegisterUserPO> contestRegisterUserPOS = contestRegisterMapper.selectByExample(example);

        for (ContestRegisterUserPO item : contestRegisterUserPOS) {


            ContestRegisterUserVO contestRegisterUser = new ContestRegisterUserVO();
            contestRegisterUser.setId(item.getId());
            contestRegisterUser.setContestId(item.getContestId());



            ContestInfoPO contestInfoPO = contestInfoService.selectByContestId(item.getContestId());
            // FIXME: 当数据库完整性被破坏之后，上一步可能会读到一个空对象。使的下面两步报空指针错误。
            String title = contestInfoPO.getTitle();
            Integer kind = contestInfoPO.getContestKind();



            contestRegisterUser.setTitle(title);
            contestRegisterUser.setContestKind(ContestKind.getNameByCode(kind));
            contestRegisterUser.setUsername(item.getUsername());
            contestRegisterUser.setRegisterTime(item.getRegisterTime());

            contestRegisterUser.setReviewTime(item.getReviewTime());
            contestRegisterUser.setReviewInfo(item.getReviewInfo());
            contestRegisterUser.setReviewStatus(ContestReviewStatus.getNameById(item.getReviewStatus()));

            result.add(contestRegisterUser);

        }
        return result;
    }

    @Override
    public Integer selectCountByConditions(Integer contestKind, Integer reviewStatus, String username) {
        Example example = new Example(ContestRegisterUserPO.class);
        Example.Criteria criteria = example.createCriteria();

        if (Objects.nonNull(contestKind)) {
            criteria.andEqualTo("contestKind", contestKind);
        }
        if (Objects.nonNull(reviewStatus)) {
            criteria.andEqualTo("reviewStatus", reviewStatus);
        }
        if (Objects.nonNull(username)) {
            criteria.andLike("username", username);
        }
        return contestRegisterMapper.selectCountByExample(example);
    }

    @Override
    public Integer updateReviewStatus(ContestRegisterUserPO contestRegisterUserPO) {
        Example example = new Example(ContestRegisterUserPO.class);
        example.createCriteria().andEqualTo("id", contestRegisterUserPO.getId());
        return contestRegisterMapper.updateByExampleSelective(contestRegisterUserPO, example);
    }

    @Override
    public ContestRegisterUserPO selectByUsername(String username,Integer contestId){
        Example example = new Example(ContestRegisterUserPO.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("username",username);
        criteria.andEqualTo("contestId",contestId);

        return contestRegisterMapper.selectOneByExample(example);
    }

    @Override
    public List<UserSignUpVO> getSignUpList(Integer pageNum, Integer pageSize, Integer ContestId){
        PageHelper.startPage(pageNum, pageSize);

        //读取参与比赛的人的基本信息
        Example example = new Example(VContestUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("contestId",ContestId);
        List<VContestUser> vContestUsers = vContestUserMapper.selectByExample(example);


        //读取实名认证信息并生成哈希表
        List<UserRealNameAuth> userRealNameAuths = userRealNameAuthMapper.selectAll();
        HashMap<String, UserRealNameAuth> userRealNameAuthmap = new HashMap<>();
        for(UserRealNameAuth item:userRealNameAuths){
            userRealNameAuthmap.put(item.getUsername(),item);
        }

        //生成
        List<UserSignUpVO> userSignUpVOS = new ArrayList<>();

        //把temp放在for循环外，则进入列表的类和原本的类相互影响
        //把temp放在for循环内，每次循环生成新的temp，则互不影响。
        for(VContestUser item: vContestUsers ){
            UserSignUpVO temp = new UserSignUpVO();
            temp.setUsername(item.getUsername());
            temp.setNickname(item.getNickname());
            temp.setRating(item.getRating());
            temp.setRegisterTime(item.getRegisterTime());
            temp.setReview_status(item.getReview_status());

            UserRealNameAuth temp2 = userRealNameAuthmap.get(item.getUsername());
            if(temp2 != null){
                temp.setCollege(temp2.getCollege());
                temp.setRealname(temp2.getName());
                temp.setSchool(temp2.getSchool());
                temp.setSex(temp2.getSex());
                temp.setSClass(temp2.getSClass());
                temp.setSNumber(temp2.getSNumber());
                temp.setScience(temp2.getScience());
            }

            userSignUpVOS.add(temp);
        }
        return userSignUpVOS;
    }

    @Override
    public int getCount(Integer ContestId){
        Example example = new Example(VContestUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("contestId",ContestId);
        return vContestUserMapper.selectCountByExample(example);
    }

    @Override
    public int insertUserSign(String username,Integer contestId){
        Example example = new Example(ContestRegisterUserPO.class);

        ContestRegisterUserPO contestRegisterUserPO = new ContestRegisterUserPO();
        contestRegisterUserPO.setUsername(username);
        contestRegisterUserPO.setContestId(contestId);
        contestRegisterUserPO.setRegisterTime(new Date());
        contestRegisterUserPO.setReviewStatus(0);

        example.createCriteria().andEqualTo("username",username)
                .andEqualTo("contestId",contestId)
                .andEqualTo("registerTime",new Date())
                .andEqualTo("reviewStatus",0);

        if (contestRegisterMapper.selectOneByExample(example) != null){
            return 0;
        }
        return contestRegisterMapper.insertSelective(contestRegisterUserPO);
    }

}
